/**
 * @author harry
 * @date 2021/12/7/21:28
 */

package com.controller;

import com.pojo.Contact;
import com.pojo.Group;

import java.sql.*;
import java.util.LinkedList;
import java.util.List;

public class GroupController extends Controller{

    public GroupController() {
    }

    /**
     * 查询所有分组标签
     * @return 所有分组标签的列表
     */
    public List<Group> getAllGroups() {
        try {
            List<Group> groups = new LinkedList<>();
            Connection conn = null;
            Statement stmt = null;
            //STEP 2: Register JDBC driver
            Class.forName(JDBC_DRIVER);
            conn = DriverManager.getConnection(DB_URL, USER, PASS);
            stmt = conn.createStatement();
            String sql;
            sql = "SELECT name FROM `group` ";
            ResultSet rs = stmt.executeQuery(sql);
            while (rs.next()) {
                Group group = new Group(rs.getString("name"));
                groups.add(group);
            }
            //STEP 6: Clean-up environment
            rs.close();
            stmt.close();
            conn.close();
            return groups;
        }catch (SQLException se){
            se.printStackTrace();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 添加分组标签
     * @param group 新分组标签
     * @return 添加是否成功
     */
    public boolean addGroup(String group) {
        try {
            Connection conn = null;
            Statement stmt = null;
            //STEP 2: Register JDBC driver
            Class.forName(JDBC_DRIVER);
            conn = DriverManager.getConnection(DB_URL, USER, PASS);
            stmt = conn.createStatement();
            String sql;
            sql = ("INSERT INTO `group`" + "(name) VALUES (?)");
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, group);
            pstmt.execute();
//            ResultSet rs = pstmt.executeQuery();
//            //STEP 6: Clean-up environment
//            rs.close();
            stmt.close();
            conn.close();
        }catch (SQLException se){
            se.printStackTrace();
            return false;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 删除分组标签
     * @param group 待删除分组标签
     * @return 删除是否成功
     */
    public boolean deleteGroup(String group) {
        try {
            Connection conn = null;
            Statement stmt = null;
            //STEP 2: Register JDBC driver
            Class.forName(JDBC_DRIVER);
            conn = DriverManager.getConnection(DB_URL, USER, PASS);
            stmt = conn.createStatement();
            String sql;
            sql = ("DELETE FROM `group`" + "WHERE name = ?");
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, group);
            pstmt.execute();
            sql = ("UPDATE `contact` SET `group` = ?" + " WHERE `group` = ?");
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, "");
            pstmt.setString(2, group);
            pstmt.execute();
            stmt.close();
            conn.close();
        }catch (SQLException se){
            se.printStackTrace();
            return false;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }


    /**
     * 修改分组标签
     * @param oldGroup 原分组标签
     * @param newGroup 新分组标签
     * @return 修改是否成功
     */
    public boolean modifyGroup(Group oldGroup, Group newGroup) {
        try {
            Connection conn = null;
            Statement stmt = null;
            //STEP 2: Register JDBC driver
            Class.forName(JDBC_DRIVER);
            conn = DriverManager.getConnection(DB_URL, USER, PASS);
            stmt = conn.createStatement();
            String sql;
            sql = ("UPDATE `group` SET name = ? WHERE name = ?" );
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, newGroup.getValue());
            pstmt.setString(2, oldGroup.getValue());
            pstmt.execute();
            sql = ("UPDATE `contact` SET `group` = ? WHERE `group` = ?" );
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, newGroup.getValue());
            pstmt.setString(2, oldGroup.getValue());
            pstmt.execute();
            //STEP 6: Clean-up environment
            stmt.close();
            conn.close();
        }catch (SQLException se){
            se.printStackTrace();
            return false;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
